#!/bin/bash
#leao:Fri Aug 23 06:06:26 PM CST 2024
# 定义build函数  
function build {  
    local filename="$1.c"  
    # 如果文件已存在，先删除它（如果需要重新构建）  
    if [ -e "$filename" ]; then  
        rm "$filename"  
    fi  
    # 创建新文件并写入C程序模板  
    touch "$filename"  
    echo "#include <stdio.h>
#include <stdlib.h>
#define THREAD_ERROR_CHECK(ret, msg)              \
    {                                             \
        if (ret != 0)                             \
        {                                         \
            fprintf(stderr,"%s:%s", msg, strerror(ret)); \
        }                                         \
    }
#define ARGS_CHECK(argc, num)                 \
    {                                         \
        if (argc != num)                      \
        {                                     \
            fprintf(stderr, "args error!\n"); \
            return -1;                        \
        }                                     \
    }
#define ERROR_CHECK(ret, num, msg)  \
{                                   \
        if (ret == num)             \
        {                           \
            perror(msg);            \
            exit(1);                \
        }                           \
}   
int main(int argc, char *argv[]){  
    
   
    return 0;  
}" > "$filename"  
}  
  
# 检查是否提供了命令行参数  
if [ -z "$1" ]; then  
    echo "error: Missing argument (filename)"  
    exit 1  
fi  
  
# 检查文件是否已存在  
if [ -e "$1.c" ]; then  
    echo "${1} already exists. Rebuild (y/Y), or exit (n/N)?: "  
    read val  
    case "$val" in  
        [yY]*)  
            build "$1";;  
        [nN]*)  
            exit 0;; # 成功退出，不执行任何操作  
        *)  
            echo "Invalid input. Exiting."  
            exit 1;; # 无效输入，退出程序  
    esac  
else  
    # 如果文件不存在，直接构建  
    build "$1"  
fi

